<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class DownController extends Controller
{
    //列表
    public function getIndex()
    {
        $data = DB::table('down')->get();
        return view('Admin.down.down',['data'=>$data]);
    }

    //修改图片
    public function getEdit($id)
    {
        $data = DB::table('down')->where('id',$id)->first();
        return view('Admin.down.edit',['data'=>$data]);
    }

    //修改图片
    public function postUpdate(Request $request)
    {
        $data = $request->except('_token');
        if($request->hasFile('pic')){
           //调用方法进行头像的上传操作
           $data['pic'] = $this->upload($request);
        }
        $res = DB::table('down')->where('id',$data['id'])->update($data);
        if($res){
            return redirect('admin/down/index')->with('success','用户修改成功');
        }else{
            return back()->with('error','用户修改失败');
        }
    }

    //图片上传
    public function upload($request)
    {
        if ($request->hasfile('pic')) {
            $aa = $request->file('pic')->getClientMimeType();
            $type = array('image/png','image/gif','image/jpeg');
            if(!in_array($aa, $type)){
                die('请选择正确的文件类型');
            }
            $name = md5(time()+rand(1,999999));
            $su = $request->file('pic')->getClientOriginalExtension();
            $request->file('pic')->move('./public/Uploads',$name.'.'.$su);
            return '/public/Uploads/'.$name.'.'.$su;
        }
    }
}
